<?php
namespace App\Models\GoodsRecord;

use App\Contracts\Repositories\GoodsRecordRepository;

class GoodsRecordService
{
    private $goodsRecordRepository;

    public function __construct(GoodsRecordRepository $goodsRecordRepository)
    {
        $this->goodsRecordRepository = $goodsRecordRepository;
    }

    public function getByGoodsId($goodsId)
    {
        return $this->goodsRecordRepository->getByConditions(array('goods_id' => $goodsId));
    }

    public function createRecord($record)
    {
        $goodsInfos = $this->goodsRecordRepository->getByConditions(array('goods_id' => $record['goods_id']));
        if (is_array($goodsInfos)) {
            $goodsInfo = end($goodsInfos);
            $summary = $goodsInfo['summary'];
        } else {
            $summary = 0;
        }
        if ($record['operate'] == 'entry') {
            $record['summary'] = $record['number'] + $summary;
        } elseif ($record['operate'] == 'delivery') {
            $record['summary'] = $summary - $record['number'];
        }
        return $this->goodsRecordRepository->add($record);
    }

    public function modifyRecord($records, $conditions)
    {
    }

    public function deleteRecord($conditions)
    {
    }
}
